package com.architecture.core.init;

import com.architecture.core.service.BaseService;
import com.architecture.core.utils.ConfigUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-25
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Component
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true, timeout = 30)
public class InitDBService implements InitializingBean {

    @Resource
    private BaseService baseService;

    @Override
    public void afterPropertiesSet() throws Exception {


        //计算数据库中某一张表是否为空表，将插入初始化数据，顺序不可逆
        List<Object> groups = this.baseService.executeSQLQuery(ConfigUtil.getString(ConfigUtil.D_S_I_QUERY_GROUP_NUM));

        if(!CollectionUtils.isEmpty(groups)){
            Integer groupNum = (Integer)groups.get(0);
            if(0 == groupNum){
                //插入数据
                this.baseService.executeSQL(ConfigUtil.getStringList(ConfigUtil.D_S_I_INSERT_GROUP));
            }
        }

        List<Object> users = this.baseService.executeSQLQuery(ConfigUtil.getString(ConfigUtil.D_S_I_QUERY_USER_NUM));

        if(!CollectionUtils.isEmpty(users)){
            Integer userNum = (Integer)users.get(0);
            if(0 == userNum){
                this.baseService.executeSQL(ConfigUtil.getStringList(ConfigUtil.D_S_I_INSERT_USER));
            }
        }

    }

}
